package com.ddm.config;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.ddm.properties.OpenProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;

@Configuration
public class WxMaConfig {

    @Autowired
    private OpenProperties openProperties;

    @Bean
    public WxMaService wxMaService() {
        // 强制校验 appid 和 secret 有效性
        if (openProperties.getAppId() == null || openProperties.getAppSecret() == null) {
            throw new IllegalArgumentException("小程序 appid 或 secret 未配置");
        }

        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(openProperties.getAppId().trim()); // 去除可能的空格
        config.setSecret(openProperties.getAppSecret().trim());

        WxMaService service = new WxMaServiceImpl();
        service.setWxMaConfig(config);
        return service;
    }
}